﻿/*
 * 刘冬 博客园
 * www.cnblogs.com/GoodHelper
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web.Mvc;

using Spring.Context.Support;
using NVelocity;
using NVelocity.App;
using Spring.Template.Velocity;

using Host.ModelDTO;
using Client.ControllerBase;

namespace Client.MvcController
{
    public class ExcelController : AbstractController
    {
        /// <summary>
        /// 生成Excel
        /// </summary>
        /// <returns></returns>
        public ActionResult UserReport()
        {
            WebApplicationContext ctx = ContextRegistry.GetContext() as WebApplicationContext;

            List<UserDTO> list = this.WcfService.GetAllUser();
            var output = new
            {
                User = "刘冬",
                OutputDate = DateTime.Now,
                CopyRight = "博客园-刘冬.NET"
            };

            Hashtable modelTable = new Hashtable();
            modelTable.Add("List", list);
            modelTable.Add("Output", output);
            VelocityContext velocityContext = new VelocityContext(modelTable);
            VelocityEngine velocityEngine = ctx.GetObject("VelocityEngine") as VelocityEngine;
            string mergedTemplate = VelocityEngineUtils.MergeTemplateIntoString(velocityEngine, "User.xml", Encoding.UTF8.WebName, modelTable);
            this.HttpContext.Response.ContentType = "application/ms-excel";
            return Content(mergedTemplate);
        }
    }
}
